package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetMealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired
    private SetMealService setmealService;
    @Autowired
    private CategoryService categoryService;

    // 增加一个控制器方法，保存套餐
    // 缺少必要的POJO、DTO类
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "新增套餐接口")
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("[新增套餐 dto:{}]",setmealDto);
        // 调用SetmealService完成套餐及菜品关系的保存
        // SetmealService没有保存多表的业务方法，需要我们自己扩展
        setmealService.saveWithDish(setmealDto);
        return R.success("保存成功");
    }

    @GetMapping("/page")
    @ApiOperation("套餐分页口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true),
            @ApiImplicitParam(name = "name", value = "套餐名称", required = false)

    })
    public R<Page> findByPage(Integer page, Integer pageSize, String name) {

        log.info("[套餐分页查询 page:{},pageSize:{},name:{}]", page, pageSize, name);
        Page<Setmeal> pageInfo = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(name != null && !name.equals(""), Setmeal::getName, name);
        setmealService.page(pageInfo, queryWrapper);
        List<Setmeal> records = pageInfo.getRecords();
        long total = pageInfo.getTotal();
        List<SetmealDto> setmealDtos = new ArrayList<>();
        Page<SetmealDto> newDtoPage = new Page<>();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);
            Category category = categoryService.getById(record.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            setmealDtos.add(setmealDto);

        }
        newDtoPage.setRecords(setmealDtos);
        newDtoPage.setTotal(total);

        return R.success(newDtoPage);
    }
@CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
@ApiOperation("批量删除套餐接口")
    public R<String> deleteWithIds(@RequestParam List<Long> ids){
        log.info("[批量删除套餐],{}", Arrays.toString(ids.toArray()));
        // 这次批量删除是否涉及多表,肯定要删除套餐及套餐关系表
        setmealService.deleteWithIds(ids);
        return R.success("批量删除成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+ #setmeal.status")
    @ApiOperation("套餐条件查询接口")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
